//
//  MASTableViewImagesCell.m
//  MasonryExample
//
//  Created by 山刘 on 2020/6/11.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "MASTableViewImagesCell.h"
@interface MASTableViewImagesCell()
@property (strong, nonatomic) NSArray<NSString*> *imagesArr;
@end

@implementation MASTableViewImagesCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        _imagesArr = [NSArray array];

        self.picContentView = [LayoutUtils createView];
        [self.contentView addSubview:self.picContentView];
        [self.picContentView makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self.contentView);
        }];
        
        self.bottomView = [LayoutUtils createView];
        [self.contentView addSubview:self.bottomView];
        [self.bottomView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.picContentView.mas_bottom).insets(kPadding);
            make.height.equalTo(@100);
            make.left.bottom.right.equalTo(kPadding);
        }];
    }
    return self;
}

-(void)updateData{
    if (self.cellData.imgs && self.cellData.imgs.length > 0) {
        NSString *temptString = self.cellData.imgs;
        if ([[temptString substringFromIndex:temptString.length-1] isEqualToString:@","]) {
            temptString = [temptString substringToIndex:[temptString length]-1];
        }
        self.imagesArr = [temptString componentsSeparatedByString:@","];
        [self createPicView:self.imagesArr.count];
    }
    [super updateData];
}
- (void)createPicView:(NSInteger)itemNum {
    if (itemNum <= 0) return;
    
    for (UIView *object in self.picContentView.subviews) {
        [object removeFromSuperview];
    }
    
    //假设要显示 num 个item
    NSInteger num = itemNum;
    //每行显示的个数
    NSInteger count = 3;
    //显示的总行数
    NSInteger rowNum = (num/count) + ((NSInteger)(num%count>0));
    
    UIView *lastView = nil;
    for (int i = 0; i < rowNum; i ++) {
        NSMutableArray *masonryViewArray = [NSMutableArray array];
        for (int j = 0; j < count; j ++) {
            NSInteger currentIndex = i * count + j;
            
            UIView *view = [LayoutUtils createView];
            if (currentIndex > num-1) {
                view.backgroundColor = [UIColor clearColor];
                view.layer.borderColor = [UIColor clearColor].CGColor;
            }
            [self.picContentView addSubview:view];
            
            [masonryViewArray addObject:view];
            lastView = view;
        }
        // 固定 item 之间的间距，item 的宽或者高自动缩放
        [masonryViewArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15 leadSpacing:kPadding.left tailSpacing:kPadding.right];
        // 设置array的垂直方向的约束
        [masonryViewArray makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@((100 * i) + kPadding.top));
            make.height.equalTo(@80).priorityLow();
        }];
    }
    [self.picContentView makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView.mas_bottom).priorityLow();
    }];
}
@end
